using System;
using bitzhuwei.Compiler;

namespace bitzhuwei.GLSLFormat {
    /// <summary>
    /// Correspond to the Vn node init_declarator_list in the grammar(GLSL).
    /// </summary>
    internal partial class Vninit_declarator_list : IFullFormat {
        // [112] init_declarator_list = single_declaration ;
        // [113] init_declarator_list = init_declarator_list ',' 'identifier' ;
        // [114] init_declarator_list = init_declarator_list ',' 'identifier' array_specifier ;
        // [115] init_declarator_list = init_declarator_list ',' 'identifier' array_specifier '=' initializer ;
        // [116] init_declarator_list = init_declarator_list ',' 'identifier' '=' initializer ;


        private readonly List<IFullFormat> postfix = new();

        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;
        public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
            var config = new BlankConfig();
            context.PrintBlanksAnd(this.r0, preConfig, writer);
            var postfixCount = this.postfix.Count;
            for (var i = 0; i < postfixCount; i++) {
                if (i == 0) {
                    context.PrintCommentsBetween(this.postfix[i], this.r0, config, writer);
                }
                else {
                    context.PrintCommentsBetween(this.postfix[i], this.postfix[i - 1], config, writer);
                }
                context.PrintBlanksAnd(this.postfix[i], config, writer);
            }
        }

        // [113] init_declarator_list = init_declarator_list ',' 'identifier' ;
        public void Add(Token r1, Token r0) {
            this.postfix.Add(new Postfix0(r1, r0));
            this._scope.end = r0.index;
        }
        class Postfix0 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                var config = new BlankConfig();
                context.PrintBlanksAnd(r1, preConfig, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
            public readonly Token r1;
            public readonly Token r0;
            public Postfix0(Token r1, Token r0) {
                this.r1 = r1;
                this.r0 = r0;
                this._scope = new TokenRange(r1, r0);
            }
        }

        // [114] init_declarator_list = init_declarator_list ',' 'identifier' array_specifier ;
        public void Add(Token r2, Token r1, Vnarray_specifier r0) {
            this.postfix.Add(new Postfix1(r2, r1, r0));
            this._scope.end = r0.Scope.end;
        }
        class Postfix1 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                var config = new BlankConfig();
                context.PrintBlanksAnd(r2, preConfig, writer);
                context.PrintCommentsBetween(r2, r1, config, writer);
                context.PrintBlanksAnd(r1, config, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
            public readonly Token r2;
            public readonly Token r1;
            public readonly Vnarray_specifier r0;
            public Postfix1(Token r2, Token r1, Vnarray_specifier r0) {
                this.r2 = r2;
                this.r1 = r1;
                this.r0 = r0;
                this._scope = new TokenRange(r2, r0);
            }
        }

        // [115] init_declarator_list = init_declarator_list ',' 'identifier' array_specifier '=' initializer ;
        public void Add(Token r4, Token r3, Vnarray_specifier r2, Token r1, Vninitializer r0) {
            this.postfix.Add(new Postfix2(r4, r3, r2, r1, r0));
            this._scope.end = r0.Scope.end;
        }
        class Postfix2 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                var config = new BlankConfig();
                context.PrintBlanksAnd(r4, preConfig, writer);
                context.PrintCommentsBetween(r4, r3, config, writer);
                context.PrintBlanksAnd(r3, config, writer);
                context.PrintCommentsBetween(r3, r2, config, writer);
                context.PrintBlanksAnd(r2, config, writer);
                context.PrintCommentsBetween(r2, r1, config, writer);
                context.PrintBlanksAnd(r1, config, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
            public readonly Token r4;
            public readonly Token r3;
            public readonly Vnarray_specifier r2;
            public readonly Token r1;
            public readonly Vninitializer r0;
            public Postfix2(Token r4, Token r3, Vnarray_specifier r2, Token r1, Vninitializer r0) {
                this.r4 = r4;
                this.r3 = r3;
                this.r2 = r2;
                this.r1 = r1;
                this.r0 = r0;
                this._scope = new TokenRange(r4, r0);
            }
        }

        // [116] init_declarator_list = init_declarator_list ',' 'identifier' '=' initializer ;
        public void Add(Token r3, Token r2, Token r1, Vninitializer r0) {
            this.postfix.Add(new Postfix3(r3, r2, r1, r0));
            this._scope.end = r0.Scope.end;
        }
        class Postfix3 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                var config = new BlankConfig();
                context.PrintBlanksAnd(r3, preConfig, writer);
                context.PrintCommentsBetween(r3, r2, config, writer);
                context.PrintBlanksAnd(r2, config, writer);
                context.PrintCommentsBetween(r2, r1, config, writer);
                context.PrintBlanksAnd(r1, config, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
                context.PrintBlanksAnd(r0, config, writer);
            }
            public readonly Token r3;
            public readonly Token r2;
            public readonly Token r1;
            public readonly Vninitializer r0;
            public Postfix3(Token r3, Token r2, Token r1, Vninitializer r0) {
                this.r3 = r3;
                this.r2 = r2;
                this.r1 = r1;
                this.r0 = r0;
                this._scope = new TokenRange(r3, r0);
            }
        }


        public readonly Vnsingle_declaration r0;
        public Vninit_declarator_list(Vnsingle_declaration r0) {
            this.r0 = r0;
            this._scope = new TokenRange(r0);
        }


    }
}
